# ------------------------------------------------------------------------------------------------
### Generate Table 7: Effects by resolution type
# ------------------------------------------------------------------------------------------------

# Load data 
load("../data/results/sdid_results.Rda")

# Filter and prepare data - need to handle type filtering differently
filtered_results <- sdid_results %>%
  filter(
    outcome %in% c("Overall Turnout", "Party Turnout"),
    party %in% c("Opposition", "Overall Turnout", "Law and Justice (PiS)"),
    sum_level == "Any Level",
    sum_type == "Any Type",
    level == "Any Level",
    type != "Any Type",  # This is the key filter - exclude "Any Type" to get specific types
    control_sample == "notyettreated",
    no300k_sample == "Full Sample",
    noproposed_sample == "Full Sample"
  ) %>%
  mutate(
    outcome = case_when(
      outcome == "Party Turnout" & party == "Opposition" ~ "Opposition Turnout",
      outcome == "Party Turnout" & party == "Law and Justice (PiS)" ~ "Government Turnout",
      TRUE ~ outcome
    ),
    outcome = factor(outcome, levels = c("Overall Turnout", "Opposition Turnout", "Government Turnout"))
  ) %>%
  arrange(outcome, type)

# Generate and save table
latex_table <- create_latex_table(
  filtered_results,
  group_var = "outcome",
  row_var = "type",
  caption = "Synthetic Difference-in-Differences Estimates by Resolution Type",
  label = "tab:sdid_by_resolution",
  col_names = c("Outcome", "Resolution Type", "Estimate (SE)", "N")
)

save_table(latex_table, "../output/tables/table7.txt")
